/*
* @Author: dengjiayao
* @Date:   2017-08-11 11:00:25
* @Last Modified by:   dengjiayao
* @Last Modified time: 2017-08-11 17:39:18
*/

import Vue from 'vue'
import queryString from 'query-string'

const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage/tingshei/expert'

const categoryResource = Vue.resource(API_ROOT + '/categoryList')
export const getExpertCategory = () => {
  return categoryResource.get()
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const listResource = Vue.resource(API_ROOT + '/list')
export const getExpertList = ({dispatch}, params) => {
  return listResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('EXPERT_NEED_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const saveResource = Vue.resource(API_ROOT + '/save')
export const saveExpert = ({dispatch}, params) => {
  return saveResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('EXPERT_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const updateResource = Vue.resource(API_ROOT + '/update')
export const updateExpert = ({dispatch}, params) => {
  return updateResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('EXPERT_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const deleteResource = Vue.resource(API_ROOT + '/delete')
export const deleteExpert = ({dispatch}, params) => {
  return deleteResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('EXPERT_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const detailResource = Vue.resource(API_ROOT + '/detail')
export const getExpertDetail = ({dispatch}, params) => {
  return detailResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

const importResource = Vue.resource(API_ROOT + '/import')
export const importExpert = ({dispatch}, params) => {
  return importResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('EXPERT_NEED_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

export const exportExpert = ({dispatch}, params) => {
  let query = queryString.stringify(params)
  window.open(API_ROOT + '/export?' + query)
}
